/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.controllers;

import com.insurance.cis.models.Gender;
import com.insurance.cis.models.Group;
import com.insurance.cis.scos.EmployeeSearchCondition;
import com.insurance.cis.utils.CisConstants;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import com.insurance.cis.views.EmployeeManagementJPanelView;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmployeeController {

    final Pattern pattern = Pattern.compile(CisConstants.DATE_PATTERN_COMPILE);
    EmployeeManagementJPanelView empManagementJpnl;

    public EmployeeController(EmployeeManagementJPanelView aThis) {
        empManagementJpnl = (EmployeeManagementJPanelView) aThis;
    }

    public EmployeeSearchCondition validateSearchConditions() {
        if (empManagementJpnl == null) {
            return null;
        }
        Date dob;
        try {
            dob = validateDate(empManagementJpnl.getTxtEmployeeDob().getText().trim());
        } catch (ParseException ex) {
            empManagementJpnl.getTxtEmployeeDob().requestFocus();
            MessageDialogUtils.dialogError(empManagementJpnl, "Birthday must be in format dd/mm/yyyy!");
            return null;
        }
        if (dob != null) {
            Date today = new Date();
            if (dob.after(today)) {
                MessageDialogUtils.dialogError(empManagementJpnl, "Invalid birthday value!");
                empManagementJpnl.getTxtEmployeeDob().requestFocus();
                return null;
            }
        }
        EmployeeSearchCondition sco = new EmployeeSearchCondition(
                empManagementJpnl.getTxtEmployeeID().getText().trim(), 
                empManagementJpnl.getTxtEmployeeName().getText().trim(), 
                empManagementJpnl.getTxtUsername().getText().trim(), 
                getGender(), dob, 
                ((Group)empManagementJpnl.getCbxGroup().getSelectedItem()).getId());
        
        return sco;
    }

    private String getGender() {
        if (empManagementJpnl.getRbtnMale().isSelected()) {
            return Gender.MALE.getDesc();
        } else if (empManagementJpnl.getRbtnFemale().isSelected()) {
            return Gender.FEMALE.getDesc();
        }
        return CisConstants.BLANK;
    }
    
    private Date validateDate(String date) throws ParseException {
        Date dateRet = null;

        if (!date.isEmpty()) {
            if (date.trim().length() != 10) {
                throw new ParseException("Date parse exception", 0);
            }
            Matcher matcher = pattern.matcher(date.trim());
            if (!matcher.matches()) {
                throw new ParseException("Date parse exception", 0);
            }
            dateRet = Utils.parseDate(date.trim(),
                    CisConstants.DATE_DD_MM_YYYY);
        }
        return dateRet;
    }

}
